package com.peng.user.controller;


import com.peng.user.entity.UcenterMember;
import com.peng.user.entity.vo.LoginVo;
import com.peng.user.entity.vo.RegisterVo;
import com.peng.user.service.UcenterMemberService;
import com.peng.utils.JwtUtils;
import com.peng.utils.Result;
import io.jsonwebtoken.Jwt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author Dengp
 * @since 2022-06-19
 */
@RestController
@RequestMapping("/ucenter/member")
@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService ucenterMemberService;

    @PostMapping("/login")
    public Result login(@RequestBody LoginVo loginVo){
        String token=ucenterMemberService.login(loginVo);
        HashMap<String, Object> map = new HashMap<>();
        map.put("token",token);
        return Result.ok().data(map);
    }

    @PostMapping("/register")
    public Result register(@RequestBody RegisterVo registerVo){
        ucenterMemberService.register(registerVo);
        return Result.ok();
    }

    @GetMapping("/getUserInfo")
    public Result getUserInfo(HttpServletRequest request){
        String userId = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember user = ucenterMemberService.getById(userId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("userInfo",user);
        return Result.ok().data(map);
    }

    @GetMapping("/getUserInfoClient/{id}")
    public UcenterMember getUserInfoClient(@PathVariable("id")String id){
        UcenterMember user = ucenterMemberService.getById(id);
        return user;
    }

    //根据日期统计当天注册人数
    @GetMapping("/getRegisterCount/{day}")
    public int getRegisterCount(@PathVariable String day){
        int count= ucenterMemberService.getRegisterCount(day);
        return count;
    }
}

